package com.orhon.smartcampus.modules.student.mutation;

import java.util.ArrayList;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.GradesDao;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.material.dao.BuildingsDao;
import com.orhon.smartcampus.modules.material.dao.FloorDao;
import com.orhon.smartcampus.modules.material.dao.RoomDao;
import com.orhon.smartcampus.modules.material.dao.UnitDao;
import com.orhon.smartcampus.modules.student.dao.ArrivesDao;
import com.orhon.smartcampus.modules.student.dao.EclassDao;
import com.orhon.smartcampus.modules.student.entity.Arrives;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.gqlInputModel.GQLEclassInput;
import com.orhon.smartcampus.modules.student.inputMapper.EclassInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddEclassMutation<E> implements DataFetcher {



	@Autowired
	EclassDao eclassDao;

	@Autowired
	SchoolsDao schoolsDao;

	@Autowired
	GradesDao gradesDao;

	@Autowired
	ArrivesDao arrivesDao;

	@Autowired
	BuildingsDao buildingsDao;

	@Autowired
	UnitDao unitDao;

	@Autowired
	FloorDao floorDao;

	@Autowired
	RoomDao roomDao;

	@Autowired
	TokenService tokenService;

	/**
	 * 新增/修改班级信息
	 * editor : ths
	 */
	 @Override
	 public Object get(DataFetchingEnvironment environment) throws Exception {

		 //String token = (String) SecurityUtils.getSubject().getPrincipal();
		 String token = tokenService.getSystemToken();
		 Long schoolId = JwtUtils.getSchoolId(token);
		 GQLEclassInput input = EclassInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));
		 Eclass eclass = null;
		 if (input.getId() != null){
			 eclass = eclassDao.get(Eclass.class , input.getId());
			 eclass.setUpdatedAt(new Date());
		 }
		 else{
			 eclass = new Eclass();
			 eclass.setCreatedAt(new Date());
		 }
		 if(schoolId != null) {
			 if(input.getSchool_id()!=null) {
				 eclass.setBaseSchoolsBySchoolId(schoolsDao.get(Schools.class,input.getSchool_id()));
			 }else {
				 eclass.setBaseSchoolsBySchoolId(schoolsDao.get(Schools.class,schoolId));
			 }
		 }
		 if(input.getGrade_id() != null) {
			 eclass.setBaseGradesByGradeId(gradesDao.get(Grades.class, input.getGrade_id()));
		 }
		 if(input.getArrives_id() != null) {
			 eclass.setStudentArrivesByArrivesId(arrivesDao.get(Arrives.class, input.getArrives_id()));
		 }
		 if(input.getCode() != null){

			 eclass.setEclassCode(input.getCode());
		 }
		 if(input.getStatus() != null){

			 eclass.setClassStatus(input.getStatus());
		 }
		 if(input.getKind_dict() != null){

			 eclass.setClassKindDict(input.getKind_dict());
		 }
		 if(input.getNature() != null){

			 eclass.setClassNature(input.getNature());
		 }
		 //        if(input.getBuilding_id() != null) {
		 //            eclass.setMaterialBuildingsByBuildingsId(buildingsDao.get(Buildings.class, input.getBuilding_id()));
		 //        }
		 //        if(input.getUnit_id() != null) {
		 //            eclass.setMaterialUnitByUnitId(unitDao.get(Unit.class, input.getUnit_id()));
		 //        }
		 //        if(input.getFloor_id() != null) {
		 //            eclass.setMaterialFloorByFloorId(floorDao.get(Floor.class, input.getFloor_id()));
		 //        }
		 //        if(input.getClassroom_id() != null) {
		 //        	eclass.setRoom(roomDao.get(Room.class, input.getClassroom_id()));
		 //        }
		 if(input.getGraduated_flag() != null){

			 eclass.setGraduatedFlag(input.getGraduated_flag());
		 }
		 if(input.getEclass_order() != null){

			 eclass.setEclassOrder(input.getEclass_order());
		 }
		 if(input.getEclass_number() != null){

			 eclass.setEclassNumber(input.getEclass_number());
		 }
		 if(input.getIoc_path() != null){

			 eclass.setIocPath(input.getIoc_path());
		 }
		 if(input.getLogo()!= null){

			 eclass.setLogo(input.getLogo());
		 }
		 if(input.getName() != null){

			 eclass.setEclassName(JSON.toJSONString(input.getName()));
		 }
		 if(input.getIntro() != null){

			 eclass.setIntroduction(JSON.toJSONString(input.getIntro()));
		 }
		 if(input.getTeacher_comment() != null){

			 eclass.setTeacherComment(JSON.toJSONString(input.getTeacher_comment()));
		 }
		 if(input.getHead_teacher_message() != null){

			 eclass.setHeadteacherMessage(JSON.toJSONString(input.getHead_teacher_message()));
		 }

		 try{
			 eclassDao.merge(eclass);
		 }
		 catch (Exception e){
			 return Result.error(500,""+e);
		 }
		 ArrayList<Eclass> list = new ArrayList<Eclass>();
		 list.add(eclass);
		 return R.ok().put("data", list);
	 }
}

